package com.obrempon.credit.market.ui.fragment

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.View
import com.obrempon.credit.market.R
import com.obrempon.credit.market.common.CommonFragment
import com.obrempon.credit.market.configs.ObremponConfigs
import com.obrempon.credit.market.databinding.ObremponUserCenterFragmentBinding
import com.obrempon.credit.market.event.ObremponEventData
import com.obrempon.credit.market.event.ObremponEventPointType
import com.obrempon.credit.market.ui.activity.ObremponUserFeedbackAct
import com.obrempon.credit.market.ui.activity.ObremponUserProfileAct
import com.obrempon.credit.market.ui.activity.ObremponSettingAct
import com.obrempon.credit.market.ui.activity.ObremponWebviewAct
import com.obrempon.credit.market.utils.ObremponImageUtil
import com.obrempon.credit.market.utils.helper.ObremponCustomDataHelper
import com.obrempon.credit.market.utils.shared.ObremponSharedHelper
import com.obrempon.credit.market.utils.shared.ObremponSharedKey

/**
 * ObremponCredit
 * @Author wwdeng
 * @CreateTime: 2024/12/10 14:53
 * @Annotation：我的
 */
class ObremponUserCenterFragment(): CommonFragment() {
    lateinit var binding: ObremponUserCenterFragmentBinding

    override fun getObremponRootView(inflater: LayoutInflater): View {
        binding = ObremponUserCenterFragmentBinding.inflate(layoutInflater)
        return binding.root
    }

    override fun initObremponView() {
        setObremponTitleBarVisibility(View.VISIBLE)
        setObremponTextTitleVisibility(View.GONE)
        setObremponBackBtnVisibility(View.INVISIBLE)
        setObremponRightBtnRes(R.mipmap.obrempon_customer_icon)
        rootViewBinding.rootLayout.setBackgroundColor(getColor(R.color.color_ff0569ff))
        initMeRefrashView()
    }

    override fun initObremponListener() {
        rootViewBinding.rootCustomer.setOnClickListener {
            showCustomerDialog("User Center Page")
            addEventPointRequestAction(ObremponEventPointType.CLICK.OBREMPON_CLICK_CUSTOMER,"User Center Page")
        }
        binding.userCenterAccountLayout.setOnClickListener {
            onIntent(ObremponUserProfileAct::class.java)
        }
        binding.userCenterProfileLayout.setOnClickListener {
            binding.userCenterAccountLayout.callOnClick()
        }
        binding.userCenterPrivacyPolicyLayout.setOnClickListener {
            val showTitle = getString(R.string.obrempon_privacy_policy_title)
            val linkUrl = ObremponConfigs.OBREMPON_PRIVACY_POLICY_ZMB_LINK
            ObremponWebviewAct.start(requireContext(), showTitle, linkUrl)
        }
        binding.userCenterFeedbackLayout.setOnClickListener {
            onIntent(ObremponUserFeedbackAct::class.java)
        }
        binding.userCenterAppSettingLayout.setOnClickListener {
            onIntent(ObremponSettingAct::class.java)
        }
        binding.questionLayout.setOnClickListener {
            binding.userCenterFeedbackLayout.callOnClick()
        }
    }

    @SuppressLint("SetTextI18n")
    override fun takeObremponData() {
        addEventPointRequestAction(ObremponEventPointType.PAGE.OBREMPON_PAGE_USER_CENTER_START)
        binding.textUserMobile.text = "+${ObremponSharedHelper.queryObremponSharedString(ObremponSharedKey.OBREMPON_TIMEZONE_CODE)} ${ObremponSharedHelper.queryObremponSharedString(ObremponSharedKey.OBREMPON_MOBILE)}"
        //加载保存自定义数据
        ObremponCustomDataHelper.queryObremponCustomDataAction()
    }

    /* 初始化刷新 */
    private fun initMeRefrashView() {
        binding.meRefreshLayout.setColorSchemeResources(R.color.theme,R.color.purple_500)
        binding.meRefreshLayout.setOnRefreshListener {
            ObremponCustomDataHelper.queryObremponCustomDataAction()
        }
    }

    override fun onObremponPublishEvent(event: ObremponEventData) {
        super.onObremponPublishEvent(event)
        if (event.eventType == ObremponEventData.TYPE.OBREMPON_CUSTOM_DATA_REFRESHED){
            hideObremponLoading()

            val avatarUrl = ObremponCustomDataHelper.queryObremponCustomUserAvatarUrl()
            if (avatarUrl.isNotEmpty()){
                ObremponImageUtil.instance.loadImageUrl(requireContext(),binding.imgUserHeader,avatarUrl)
            } else {
                binding.imgUserHeader.setImageResource(R.mipmap.obrempon_holder)
            }

            binding.textUserName.text = ObremponCustomDataHelper.queryObremponCustomUserName()

        }
    }

    override fun hideObremponLoading() {
        super.hideObremponLoading()
        binding.meRefreshLayout.isRefreshing = false
    }

    override fun onDestroy() {
        super.onDestroy()
        addEventPointRequestAction(ObremponEventPointType.PAGE.OBREMPON_PAGE_USER_CENTER_END)
    }
}